<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统-登录</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="UserName">
                    <el-input v-model="param.UserName" placeholder="UserName">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="Password">
                    <el-input type="password" placeholder="Password" v-model="param.Password"
                        @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <div id="main">
                        <div class="item">
                            <el-col :span="16">
                                <el-input placeholder="Password" v-model="identifyCode" @keyup.enter.native="submitForm()">
                                    <el-button slot="prepend" icon="el-icon-tickets"></el-button>
                                </el-input>
                            </el-col>
                            <el-col :span="8">
                                <span class="code" @click="refreshCode">
                                    <IdentifyCode :identify-code="identifyCode" :content-width="96" :content-height="32">
                                    </IdentifyCode>
                                </span>
                            </el-col>
                        </div>
                    </div>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <div style="margin: auto;">
                    <span style="margin-left: 0%">
                        <router-link to="/register" style="color:darkturquoise">立即注册</router-link>
                    </span>
                    <span style="margin-left: 55%">
                        <router-link to="/findPassword" style="color:rgb(61, 69, 70)">找回密码</router-link>
                    </span>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { UserLogin } from '@/api/User.js';
import IdentifyCode from "vue-identifycode";
export default {
    data: function () {
        return {
            param: {
                UserName: '小王',
                Password: '123',
                ValidateCode: '1111'
            },
            rules: {
                UserName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                Password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            },
            identifyCode: ''
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    //console.log(this.param);
                    //调用登录接口，来完成登录
                    UserLogin(this.param).then((res) => {
                        if (res.Code == 200) {
                            //给vuex赋jwt值
                            this.$store.commit('saveToken', res.Data.TokenInfo.TokenStr);
                            localStorage.setItem('saveToken', res.Data.TokenInfo.TokenStr);
                            //保存jwt过期时间
                            this.$store.commit('saveExpires', res.Data.TokenInfo.Expires);
                            localStorage.setItem('saveExpires', res.Data.TokenInfo.Expires);
                            //console.log(this.$store.state.Expires)
                            this.$message.success('登录成功');
                            // localStorage.setItem('ms_username', this.param.UserName);
                            localStorage.setItem('ms_username', this.param.UserName);
                            localStorage.setItem('userId', res.Data.Id);
                            console.log("当前令牌" + localStorage.getItem('saveToken'))
                            //取数据
                            // console.log(this.$store.state.token)
                            this.$router.push('/');
                        } else {
                            this.$message.error(res.Message)
                        }
                    });
                }
                else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //生成验证码
        createCode() {
            let code = "";
            const codeLength = 4; // 验证码的长度
            // eslint-disable-next-line no-array-constructor
            const random = new Array(2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G",
                "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
            ); // 随机数
            for (let i = 0; i < codeLength; i++) {
                // 循环操作
                const index = Math.floor(Math.random() * 32); // 取得随机数的索引（0~35）
                code += random[index]; // 根据索引取得随机数加到code上
            }
            return code;
        },
        //验证码刷新方法
        refreshCode() {
            this.identifyCode = this.createCode();
        }
    },
    name: "IdentifyCodeTest",
    components: { IdentifyCode },
    created() {
        this.refreshCode();
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}

.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}

.ms-content {
    padding: 30px 30px;
}

.login-btn {
    text-align: center;
}

.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}

.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>